#include "../import/head.h"
#include "session.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_net/head.h"

namespace xos_sgsj
{
    session::session()
    {
        m_eType = TYPE_TCP_DATA;
		m_bNeedClose = false;
        m_bQuiting = false;
        m_bTimeout = false;

        m_tLastHeartTimestampS = 0;
        net_timeout_s = 0;
        m_nRefCount = 0;

        m_pEncrypt = 0;
        m_pQueue = 0;

        m_pTcp = 0;
        m_pUdp = 0;
    }

    session::~session()
    {
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int session::init_add_ref( int nAddNum )
    {
        int ret = 0;

        m_nRefCount += nAddNum;

        return ret;
    }

    int session::add_ref( int nAddNum )
    {
        int ret = 0;

        if( 0 == m_nRefCount )
        {
            ret = 1;
        }
        else
        {
            m_nRefCount += nAddNum;
        }

        return ret;
    }

    int session::sub_ref( int nSubNum )
    {
        int ret = 0;

        if( 0 == ret )
        {
            m_nRefCount -= nSubNum;
        }

        if( ( 0 == ret ) && ( 0 == m_nRefCount ) )
        {
            post_close();
            ret = 1;
        }

        return ret;
    }

    int session::post_send( data * pData )
    {
        int ret = 0;

        if( ( 0 == ret ) && m_pTcp )
        {
            pData->m_pTcp = m_pTcp;
            net_tcp obj;
            obj.tcp_post_send( pData );
        }

        if( ( 0 == ret ) && m_pUdp )
        {
            pData->m_pUdp = m_pUdp;
            net_udp obj;
            obj.udp_post_send( pData );
        }

        return ret;
    }

    int session::post_close()
    {
        int ret = 0;

        if( ( 0 == ret ) && m_pTcp )
        {
            net_tcp obj;
            obj.tcp_post_close( m_pTcp );
        }

        if( ( 0 == ret ) && m_pUdp )
        {
        }

        return ret;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

	int session::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            ret = g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_QUEUE, ( void ** )&m_pQueue );
        }

        if( 0 == ret )
        {
            ret = g_pSysData->m_os.m_pEncryptMgr->create( &m_pEncrypt );
        }

        if( 0 == ret )
        {
            m_pEncrypt->set_aes_key( INIT_PASSWORD, g_pSysData->m_os.m_pCrt->strlen( INIT_PASSWORD ) );
        }

        return ret;
    }

    int session::term()
    {
        int ret = 0;

        if( m_pEncrypt )
        {
            m_pEncrypt->release();
            m_pEncrypt = 0;
        }

        if( m_pQueue )
        {
            m_pQueue->release();
            m_pQueue = 0;
        }

        m_eType = TYPE_TCP_DATA;
		m_bNeedClose = false;
        m_bQuiting = false;
        m_bTimeout = false;

        m_tLastHeartTimestampS = 0;
        net_timeout_s = 0;
        m_nRefCount = 0;

        m_pTcp = 0;
        m_pUdp = 0;

        return ret;
    }

    int session::release()
    {
        int ret = 0;

		term();
		put_back_lock();

        return ret;
    }

} // xos_sgsj
